package com.project.hqshorttaxi.controller;

import com.project.hqshorttaxi.dto.BoardDto;
import com.project.hqshorttaxi.dto.BoardParam;
import com.project.hqshorttaxi.dto.ShortDistanceDto;
import com.project.hqshorttaxi.dto.ShortDistanceParam;
import com.project.hqshorttaxi.entity.*;
import com.project.hqshorttaxi.service.*;
import com.project.hqshorttaxi.util.RestResult;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/board/")
public class BoardController {

    @Resource
    ShortDistanceService shortDistanceService;
    @Resource
    PlateCameraService plateCameraService;
    @Resource
    OrderService orderService;
    @Resource
    DiscountService discountService;
    @Resource
    TurnstileDataService turnstileDataService;
    @Resource
    UserSettingService userSettingService;

    /**
     * 列表
     */
    @PostMapping("page")
    public RestResult page(@RequestBody BoardParam param) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        BoardDto dto = new BoardDto();
        List<ShortDistance> shortDistanceList = shortDistanceService.boardList(param);
        dto.setShortDistanceList(shortDistanceList);
        if (shortDistanceList.size() > 0) {
            ShortDistanceDto shortDistanceDto = new ShortDistanceDto();
            ShortDistance shortDistance = shortDistanceList.get(0);
            BeanUtils.copyProperties(shortDistance, shortDistanceDto);
            if (shortDistance.getRecType() == 2) {
                UserSetting userSetting = userSettingService.getByGroupSetName(param.getGroupName(), String.valueOf(shortDistance.getRefuseType()));
                if (userSetting != null) {
                    shortDistanceDto.setGroupName(userSetting.getGroupName());
                    shortDistanceDto.setSetName(userSetting.getSetName());
                    shortDistanceDto.setSetValue(userSetting.getMemo());
                }
                if (shortDistance.getRefuseType() == 3) {
                    ShortDistanceParam param3 = new ShortDistanceParam();
                    param3.setPlateNumber(shortDistanceDto.getPlateNumber());
                    param3.setDate(sdf.format(shortDistanceDto.getReturnTime()));
                    List<ShortDistance> shortDistance3 = shortDistanceService.getExportByPlateAndDate(param3);
                    if (shortDistance3.size() > 0) {
                        List<Date> leaveTimeList = new ArrayList<>();
                        List<Date> ReturnTimeList = new ArrayList<>();
                        for (ShortDistance distance : shortDistance3) {
                            leaveTimeList.add(distance.getLeaveTime());
                            ReturnTimeList.add(distance.getReturnTime());
                        }
                        shortDistanceDto.setLeaveTimeLast(leaveTimeList);
                        shortDistanceDto.setReturnTimeLast(ReturnTimeList);
                    }
                }
                ShortDistanceParam param4 = new ShortDistanceParam();
                param4.setPlateNumber(shortDistanceDto.getPlateNumber());
                param4.setDate(sdf.format(shortDistanceDto.getReturnTime()));
                param4.setId(shortDistanceDto.getId());
                List<ShortDistance> shortDistance4 = shortDistanceService.getExportByPlateAndDate2(param4);
                dto.setAllowCount(shortDistance4.size());
            } else {
                ShortDistanceParam param3 = new ShortDistanceParam();
                param3.setPlateNumber(shortDistanceDto.getPlateNumber());
                param3.setDate(sdf.format(shortDistanceDto.getReturnTime()));
                param3.setId(shortDistanceDto.getId());
                List<ShortDistance> shortDistance3 = shortDistanceService.getExportByPlateAndDate2(param3);
                if (shortDistance3.size() > 0) {
                    List<Date> leaveTimeList = new ArrayList<>();
                    List<Date> ReturnTimeList = new ArrayList<>();
                    for (ShortDistance distance : shortDistance3) {
                        leaveTimeList.add(distance.getLeaveTime());
                        ReturnTimeList.add(distance.getReturnTime());
                    }
                    shortDistanceDto.setLeaveTimeLast(leaveTimeList);
                    shortDistanceDto.setReturnTimeLast(ReturnTimeList);
                }
                dto.setAllowCount(shortDistance3.size());
            }
            dto.setShortDistanceDto(shortDistanceDto);
            if (shortDistance.getLeaveCameraID() != null) {
                PlateCamera plateCamera = plateCameraService.getById(shortDistance.getLeaveCameraID());
                dto.setPlateCamera(plateCamera);
            }
            if (shortDistance.getOrderID() != null) {
                TaxiOrder order = orderService.getById(shortDistance.getOrderID());
                dto.setOrder(order);
            }
//            DiscountParam discountParam = new DiscountParam();
//            discountParam.setPlateNumber(shortDistance.getPlateNumber());
//            List<DiscountNum> discountNumList = discountService.list(discountParam);
//            if (discountNumList.size() > 0) {
//                dto.setDiscountNum(discountNumList.get(0));
//            }
            List<TurnstileData> turnstileDataList = turnstileDataService.getByWhiteList(shortDistance.getPlateNumber(), sdf.format(shortDistance.getReturnTime()));
            DiscountNum discountNum = new DiscountNum();
            discountNum.setPlateNumber(shortDistance.getPlateNumber());
            discountNum.setDiscountNum(turnstileDataList.size());
            dto.setDiscountNum(discountNum);
            TurnstileData turnstileData = turnstileDataService.getByShortDistanceId(shortDistance.getId());
            dto.setTurnstileData(turnstileData);
        }
        return RestResult.ok(dto);
    }

    @PostMapping("pageById")
    public RestResult pageById(@RequestBody BoardParam param) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        BoardDto dto = new BoardDto();
        ShortDistance shortDistance = shortDistanceService.getById(param.getId());
        ShortDistanceDto shortDistanceDto = new ShortDistanceDto();
        BeanUtils.copyProperties(shortDistance, shortDistanceDto);
        if (shortDistance.getRecType() == 2) {
            UserSetting userSetting = userSettingService.getByGroupSetName(param.getGroupName(), String.valueOf(shortDistance.getRefuseType()));
            if (userSetting != null) {
                shortDistanceDto.setGroupName(userSetting.getGroupName());
                shortDistanceDto.setSetName(userSetting.getSetName());
                shortDistanceDto.setSetValue(userSetting.getMemo());
            }
            if (shortDistance.getRefuseType() == 3) {
                ShortDistanceParam param3 = new ShortDistanceParam();
                param3.setPlateNumber(shortDistanceDto.getPlateNumber());
                param3.setDate(sdf.format(shortDistanceDto.getReturnTime()));
                List<ShortDistance> shortDistance3 = shortDistanceService.getExportByPlateAndDate(param3);
                if (shortDistance3.size() > 0) {
                    List<Date> leaveTimeList = new ArrayList<>();
                    List<Date> ReturnTimeList = new ArrayList<>();
                    for (ShortDistance distance : shortDistance3) {
                        leaveTimeList.add(distance.getLeaveTime());
                        ReturnTimeList.add(distance.getReturnTime());
                    }
                    shortDistanceDto.setLeaveTimeLast(leaveTimeList);
                    shortDistanceDto.setReturnTimeLast(ReturnTimeList);
                }
            }
            ShortDistanceParam param4 = new ShortDistanceParam();
            param4.setPlateNumber(shortDistanceDto.getPlateNumber());
            param4.setDate(sdf.format(shortDistanceDto.getReturnTime()));
            param4.setId(shortDistanceDto.getId());
            List<ShortDistance> shortDistance4 = shortDistanceService.getExportByPlateAndDate2(param4);
            dto.setAllowCount(shortDistance4.size());
        } else {
            ShortDistanceParam param3 = new ShortDistanceParam();
            param3.setPlateNumber(shortDistanceDto.getPlateNumber());
            param3.setDate(sdf.format(shortDistanceDto.getReturnTime()));
            param3.setId(shortDistanceDto.getId());
            List<ShortDistance> shortDistance3 = shortDistanceService.getExportByPlateAndDate2(param3);
            if (shortDistance3.size() > 0) {
                List<Date> leaveTimeList = new ArrayList<>();
                List<Date> ReturnTimeList = new ArrayList<>();
                for (ShortDistance distance : shortDistance3) {
                    leaveTimeList.add(distance.getLeaveTime());
                    ReturnTimeList.add(distance.getReturnTime());
                }
                shortDistanceDto.setLeaveTimeLast(leaveTimeList);
                shortDistanceDto.setReturnTimeLast(ReturnTimeList);
            }
            dto.setAllowCount(shortDistance3.size());
        }
        dto.setShortDistanceDto(shortDistanceDto);
        if (shortDistance.getLeaveCameraID() != null) {
            PlateCamera plateCamera = plateCameraService.getById(shortDistance.getLeaveCameraID());
            dto.setPlateCamera(plateCamera);
        }
        if (shortDistance.getOrderID() != null) {
            TaxiOrder order = orderService.getById(shortDistance.getOrderID());
            dto.setOrder(order);
        }
//        DiscountParam discountParam = new DiscountParam();
//        discountParam.setPlateNumber(shortDistance.getPlateNumber());
//        List<DiscountNum> discountNumList = discountService.list(discountParam);
//        if (discountNumList.size() > 0) {
//            dto.setDiscountNum(discountNumList.get(0));
//        }
        List<TurnstileData> turnstileDataList = turnstileDataService.getByWhiteList(shortDistance.getPlateNumber(), sdf.format(shortDistance.getReturnTime()));
        DiscountNum discountNum = new DiscountNum();
        discountNum.setPlateNumber(shortDistance.getPlateNumber());
        discountNum.setDiscountNum(turnstileDataList.size());
        dto.setDiscountNum(discountNum);
        TurnstileData turnstileData = turnstileDataService.getByShortDistanceId(shortDistance.getId());
        dto.setTurnstileData(turnstileData);

        return RestResult.ok(dto);
    }
}
